package com.baiyinet.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.Fragment;

import com.baiyinet.Constants.URL;
import com.baiyinet.biz.CurrentUser;
import com.baiyinet.biz.PageStatus;
import com.baiyinet.ui.general.GeneralActivity;
import com.baiyinet.ui.general.GeneralActivity_;
import com.baiyinet.ui.login.LoginActivity_;
import com.baiyinet.ui.login.LoginAdActivity;
import com.baiyinet.ui.login.LoginAdActivity_;
import com.baiyinet.ui.main.MainActivity_;
import com.baiyinet.ui.splash.SplashActivity;

import org.parceler.Parcels;

import cn.jpush.android.api.JPushInterface;
import myandroid.library.util.ApplicationHelper;

/**
 * @author J.Huang
 * @version 1.0
 * @since 16/12/16
 */

public class IntentUtils {
    public static void toLogin(Activity activity) {
        if (activity != null) {
            Intent intent = new Intent();

            intent.setClass(activity, LoginActivity_.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            String regId = JPushInterface.getRegistrationID(activity.getApplicationContext());
            PageStatus pageStatus = new PageStatus(URL.LOGIN + "?AppOS=Android&RegistrationId=" + regId,
                    false, "");
            intent.putExtra(PageStatus.EXTRA_NAME, Parcels.wrap(pageStatus));

            activity.startActivity(intent);
            activity.finish();
            ApplicationHelper.getInstance().clearActivity();
        }
    }

    public static void toMain(Activity activity) {
        if (activity != null) {
            toMain(activity, 0);
        }
    }

    public static void toMain(Activity activity, int tabIndex) {
        if (activity != null) {
            Intent intent = new Intent();

            intent.setClass(activity, MainActivity_.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("tabIndex", tabIndex);

            activity.startActivity(intent);
            ApplicationHelper.getInstance().clearActivity();
        }
    }

    public static void toMain(Activity activity,String adUrl) {
        if (activity != null) {
            Intent intent = new Intent();

            intent.setClass(activity, MainActivity_.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("adUrl", adUrl);

            activity.startActivity(intent);
            ApplicationHelper.getInstance().clearActivity();
        }

    }

    public static void toNewGeneralActivity(Activity activity, PageStatus pageStatus) {
        if (activity != null) {
            Intent intent = new Intent();
            intent.setClass(activity, GeneralActivity_.class);
            intent.putExtra(PageStatus.EXTRA_NAME, Parcels.wrap(pageStatus));

            activity.startActivityForResult(intent, GeneralActivity.REQUEST_NEW);
        }
    }

    public static void toNewGeneralActivity(Fragment fragment, PageStatus pageStatus) {
        if (fragment != null) {
            Intent intent = new Intent();
            intent.setClass(fragment.getActivity(), GeneralActivity_.class);
            intent.putExtra(PageStatus.EXTRA_NAME, Parcels.wrap(pageStatus));

            fragment.startActivityForResult(intent, Activity.RESULT_FIRST_USER);
        }
    }


    public static void toSystemPermissionActivity(Activity activity) {
       JumpPermissionManagement.GoToSetting(activity);
    }
}
